package com.hezuti.common.email;


import java.util.Properties;


import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

import com.hezuti.common.utils.ConfigUtil;

public class EmailService {
	
	public static  String SMTP_HOST="smtp.163.com";
	private static String host="";
	private static String from ="";
	private static String name="";
	private static String pwd="";
	private static EmailService es=null;
	private EmailService(){
		
	}
	public static  EmailService getInstance(){
		if(null==es){
			es = new EmailService();
			es.setFrom(ConfigUtil.getInstance().getString("email"));
			es.setName(ConfigUtil.getInstance().getString("email_name"));
			es.setPwd(ConfigUtil.getInstance().getString("email_pwd"));
			SMTP_HOST=ConfigUtil.getInstance().getString("email_smtp_host");
		}
		return es;
	}
	
	/**
	 * 发送email
	 * @param to
	 * @param title
	 * @param content
	 */
	public void sendEmail(String to,String title,String content){
		this.sendEmail(to, SMTP_HOST,from , name, pwd, content, title);
	}
	
	public  void sendEmail(String to,String host,String from,String name,String pwd,String content,String title) {
		Properties prop=new Properties();
		prop.put("mail.smtp.host",host);
		prop.put("mail.smtp.auth","true");
		Authenticator auth=new MyAuthenticator(name,pwd);
		Session session=Session.getDefaultInstance(prop, auth);
		session.setDebug(true);
		MimeMessage message=new MimeMessage(session);
		MimeMultipart mp = new MimeMultipart();
		MimeBodyPart body = new MimeBodyPart();
		try {
			message.setSubject(title);
			InternetAddress fromAddress =new InternetAddress(from);
			InternetAddress toAddress =new InternetAddress(to);
			message.setFrom(fromAddress);
			message.addRecipient(Message.RecipientType.TO,toAddress);
			message.saveChanges();
//			body.setContent("<meta http-equi=Content-Type content=text/html;charset=utf-8>"+content,"text/hhtml;charset=utf-8");
//			body.attachFile("F:\\mp.txt");
			body.setContent(content,"text/html;charset=UTF-8");
			
			mp.addBodyPart(body);
			message.setContent(mp);
			Transport.send(message);
			//Transport transport = session.getTransport("smtp");
			//transport.connect("", "username", "password");
		//	transport.sendMessage(message, message.getAllRecipients());
			//transport.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	public void reciveEmail(){
		String host="smtp.163.com";
		Store store=null;
		Folder folder=null;
		Properties prop = new Properties();
		Authenticator auth=new MyAuthenticator("doouxiaoyu","247821");
		Session session=Session.getDefaultInstance(prop,auth);
		session.setDebug(false);
		try {
			//Store store = session.getStore("imap");
			 store = session.getStore("pop3");
			store.connect(host, "doouxiaoyu", "247821");
			folder=store.getFolder("INBOX");
			folder.open(Folder.READ_ONLY);
			Message[] messages= folder.getMessages();
			for(int i=0;i<messages.length;i++){
				System.out.println(messages[i].getFrom()[0]+"\n"+
				                     messages[i].getSubject());
			}
			
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}finally{
			try {
				folder.close(false);
				store.close();
			} catch (MessagingException e) {
				e.printStackTrace();
			}
			
		}
		
		
		
		
	}
	public static String getHost() {
		return host;
	}
	public static void setHost(String host) {
		EmailService.host = host;
	}
	public static String getFrom() {
		return from;
	}
	public static void setFrom(String from) {
		EmailService.from = from;
	}
	public static String getName() {
		return name;
	}
	public static void setName(String name) {
		EmailService.name = name;
	}
	public static String getPwd() {
		return pwd;
	}
	public static void setPwd(String pwd) {
		EmailService.pwd = pwd;
	}
	
}	
